package com.tengju.bff.interfaces.covercharge.service.bestsign.impl.contract;

import com.tengju.bff.interfaces.covercharge.constant.ContractUserTypeEnum;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.ApplyCertificateReqVo;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.ApplyCertificateResVo;
import com.tengju.bff.interfaces.covercharge.entity.vo.contract.ApplyCertificateStatusResVo;
import com.tengju.bff.interfaces.covercharge.handler.CoverChargeException;
import com.tengju.bff.interfaces.covercharge.service.bestsign.BestSignClient;
import com.tengju.bff.interfaces.covercharge.service.bestsign.constant.BestSignBizReqUrlEnum;
import com.tengju.bff.interfaces.covercharge.service.bestsign.convert.BestSignApplyCertConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author liang_shi
 * @date 2020/7/13 16:52
 * @description 上上签申请证书服务实现
 */
@Slf4j
@Service
public class BestSignApplyCertService {


    @Resource
    private BestSignClient bestSignClient;


    /**
     * 注册用户并申请证书
     *
     * @param transId 业务流水号
     * @param reqVo   申请对象
     * @return 结果
     */
    public ApplyCertificateResVo applyCert(String transId, ApplyCertificateReqVo reqVo) {
        Map result = null;
        try {
            Map<String, Object> bizMap = null;
            if (ContractUserTypeEnum.SINGLE.getType().equals(reqVo.getUserType().getType())) {
                bizMap = BestSignApplyCertConverter.packagePersonApplyCertReq(reqVo.getAccount(), reqVo.getApplyCert(), reqVo.getPerson());
            } else {
                bizMap = BestSignApplyCertConverter.packageCompanyApplyCertReq(reqVo.getAccount(), reqVo.getApplyCert(), reqVo.getCompany());
            }
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_10, bizMap, transId);
        } catch (CoverChargeException ce) {
            throw ce;
        } catch (Exception e) {
            log.error("注册用户并申请证书异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "注册用户并申请证书异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignApplyCertConverter.packageApplyCertificateRes(data);
    }


    /**
     * 申请证书状态查询
     *
     * @param transId 业务流水号
     * @param taskId  异步申请证书返回的taskId
     * @return 结果
     */
    public ApplyCertificateStatusResVo queryApplyCertStatus(String transId, String account, String taskId) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignApplyCertConverter.packageQueryApplyCertStatus(account, taskId);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_12, bizMap, transId);
        } catch (CoverChargeException ce) {
            throw ce;
        } catch (Exception e) {
            log.error("申请证书状态查询异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "申请证书状态查询异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignApplyCertConverter.packageApplyCertificateStatusRes(data);
    }


    /**
     * 申请证书编号查询
     *
     * @param transId 业务流水号
     * @param account 用户
     * @return 结果
     */
    public String queryApplyCertId(String transId, String account) {
        Map result = null;
        try {
            Map<String, Object> bizMap = BestSignApplyCertConverter.packageQueryApplyCertId(account);
            result = bestSignClient.sendPost(BestSignBizReqUrlEnum.BIZ_REQ_11, bizMap, transId);
        } catch (CoverChargeException ce) {
            throw ce;
        } catch (Exception e) {
            log.error("申请证书编号查询异常,异常信息:", e);
            throw new CoverChargeException(CoverChargeErrorCode.SYS_ERROR.getCode(), "申请证书编号查询异常");
        }
        //结果检查
        String data = bestSignClient.checkResponse(result);
        return BestSignApplyCertConverter.packageApplyCertificateNoRes(data);
    }


}
